/*
 * @Description: 列表可编辑组件
 * @Author: seadon
 * @LastEditors: seadon
 * @Date: 2021-05-18 10:50:45
 * @LastEditTime: 2021-05-24 10:45:37
 */
import { h } from 'vue'
import EditableCell from './EditableCell.vue'
import { BasicColumn, Params } from '../../types/table'

export function renderEditCell(column: BasicColumn) {
	return ({ text: value, record, index }: Params) => {
		record.editable = true
		record.onEdit = async (edit: boolean, submit = false) => {
			if (!submit) {
				record.editable = edit
			}
			// Save
			if (!edit && submit) {
				const res = await record.onSubmitEdit?.()
				if (res) {
					record.editable = false
					return true
				}
				return false
			}
			// Cancel
			if (!edit && !submit) {
				record.onCancelEdit?.()
			}
			return true
		}

		return h(EditableCell, {
			value,
			record,
			column,
			index,
		})
	}
}
